package detector;

/**
 * Implementation de l'interface KeyboardDetector sous la forme d'un singleton.
 */
public class KeyboardDetectorImpl implements KeyboardDetector {

	private static KeyboardDetector keyboardDetectorInstance;		// Instance du singleton
		
	private boolean[] touched;										// Tableau des etats des boutons du clavier
	
	public static KeyboardDetector keyboardDetectorInstance() {
		if(keyboardDetectorInstance == null) {
			keyboardDetectorInstance = new KeyboardDetectorImpl();
		}
	
		return keyboardDetectorInstance;
	}
	
	private KeyboardDetectorImpl() {
		touched = new boolean[4];
	}
	
	@Override
	public void setButtonState(int buttonID, boolean etat) {
		touched[buttonID] = etat;
	}

	@Override
	public boolean isButtonPressed(int buttonID) {
		boolean state = touched[buttonID];
		touched[buttonID] = false;	// Une fois l'etat du bouton lu, on le replace dans un etat "non presse" dans l'optique de la
									// prochaine verification d'etat
		return state;			 
	}

}
